/*
AccountTeam_from_AgencyTeam
Version 1.0
Description: Class for insertion and updation of  AccountTeam record on insertion and updation of
             Agency_Team__c record.
Author: Pratibh Prakash
Created Date: 19 Feb-2009
*/
public class AccountTeam_from_AgencyTeam{
    
    /*
    accountTeam_on_insertAgencyTeam(Agency_Team__c[] objAgencyTeam)
    @param objAgencyTeam: Stores list of new inserted Agency_Team__c records.
    Description: Method for insertion of new AccountTeam record when new Agency_Team__c record.  in inserted.
    */
    
    public static void accountTeam_on_insertAgencyTeam(Agency_Team__c[] objAgencyTeam){
        
        String accessLevel;
        List<AccountTeamMember> newAccountTeamMemberList = new List<AccountTeamMember>();
        List<AccountShare> newAccountShareList = new List<AccountShare>();
        for(Agency_Team__c agencyTeam : objAgencyTeam){
            if(agencyTeam.User__c != null){
                if(agencyTeam.Role__c =='CL Agency Specialist' || agencyTeam.Role__c == 'CLIM Team Leader' || agencyTeam.Role__c == 'CLSM Team Leader'){
                    accessLevel = 'Edit';
                }
                else{
                    accessLevel = 'Read';
                }
                AccountTeamMember accountTeamMember = new AccountTeamMember(AccountId = agencyTeam.Agency__c, UserId = agencyTeam.User__c, TeamMemberRole = agencyTeam.Role__c);
                AccountShare accountShare = new AccountShare();
                accountShare.AccountId = agencyTeam.Agency__c;
                accountShare.AccountAccessLevel = accessLevel;
                accountShare.OpportunityAccessLevel = accessLevel;
                accountShare.CaseAccessLevel = accessLevel;
                accountShare.UserOrGroupId = agencyTeam.User__c;
                newAccountTeamMemberList.add(accountTeamMember);
                newAccountShareList.add(accountShare);     
                           
            }    
        }
              
        try{
            insert(newAccountTeamMemberList);
            insert(newAccountShareList);
            
        }
        catch(DMLException e){
            for(integer i=0; i<newAccountTeamMemberList.size(); i++){
                (newAccountTeamMemberList.get(i)).addError('Failure in Creating Account Team'+ e);
            }
        }
    }
    /*
    accountTeam_on_updateAgencyTeam(Agency_Team__c[] oldAgencyTeam, Agency_Team__c[] newAgencyTeam){
    @param oldAgencyTeam: Stores list of old Agency_Team__c reocrds before update.
    @param newAgencyTeam: Stores list of new Agency_Team__c records after update.
    Description: Method for insertion/updation of AccountTeam record when Agency_Team__c is updated.
    */
    public static void accountTeam_on_updateAgencyTeam(Agency_Team__c[] oldAgencyTeam, Agency_Team__c[] newAgencyTeam){
        
        String accessLevel;
        Map<Id,Id> accountTeamSearch_Map = new Map<Id,Id>();
        List<Agency_Team__c> upd_AgencyTeamList = new List<Agency_Team__c>();
        List<Agency_Team__c> agencyTeamList = new List<Agency_Team__c>();
        for(integer i=0 ; i< newAgencyTeam.size(); i++){
            
            if(oldAgencyTeam[i].User__c != null){
                if(oldAgencyTeam[i].Role__c != newAgencyTeam[i].Role__c){
                    accountTeamSearch_Map.put(oldAgencyTeam[i].Agency__c , oldAgencyTeam[i].User__c);
                    upd_AgencyTeamList.add(newAgencyTeam[i]);
                }
            }
            else{
                agencyTeamList.add(newAgencyTeam[i]);
                accountTeam_on_insertAgencyTeam(agencyTeamList);
            }
        }
        
        List<AccountTeamMember> for_Upd_AccTeamMem = new List<AccountTeamMember>();
        List<AccountShare> for_Upd_AccShare = new List<AccountShare>();
        AccountTeamMember[] accTeamMember = [select AccountId, UserId, TeamMemberRole from AccountTeamMember where AccountId in:  accountTeamSearch_Map.keyset() and UserId in:  accountTeamSearch_Map.values()];
        AccountShare[]  accShare = [select AccountId, AccountAccessLevel, OpportunityAccessLevel, CaseAccessLevel, UserOrGroupId from AccountShare where AccountId in: accountTeamSearch_Map.keyset() and UserOrGroupId in: accountTeamSearch_Map.values()];
       
        integer j=0;
        for(AccountTeamMember accTeamMem: accTeamMember){
           
            accTeamMem.TeamMemberRole = (upd_AgencyTeamList.get(j)).Role__c;
            for_Upd_AccTeamMem.add(accTeamMem);
            j++;
        }
        j=0;
        for(AccountShare accountShare : accShare){
            
            if((upd_AgencyTeamList.get(j)).Role__c == 'CL Agency Specialist' || (upd_AgencyTeamList.get(j)).Role__c == 'CLIM Team Leader' || (upd_AgencyTeamList.get(j)).Role__c == 'CLSM Team Leader'){
                accessLevel = 'Edit';
            }
            else{
                accessLevel = 'Read';
            }
            accountShare.AccountAccessLevel = accessLevel;
            accountShare.OpportunityAccessLevel = accessLevel;
            accountShare.CaseAccessLevel = accessLevel;
            for_Upd_AccShare.add(accountShare);
            j++;
        }
        update(for_Upd_AccTeamMem);
        update(for_Upd_AccShare);
        
   }
   
   /*
   accountTeam_on_deleteAgencyTeam(Agency_Team__c[] objAgencyTeam)
   @param objAgencyTeam: Stores list of Agency_Team__c records that are going to be deleted as returned by Trigger.old.
   Description: Method deletes Account Team on deletion of Agency_Team__c record if that record has a associated user.
   */
    public static void accountTeam_on_deleteAgencyTeam(Agency_Team__c[] objAgencyTeam){

        Map<Id,Id> accountTeamSearch_Map = new Map<Id,Id>();
       
        for(Agency_Team__c agencyTeam : objAgencyTeam){
             if(agencyTeam.User__c != null){
                 accountTeamSearch_Map.put(agencyTeam.Agency__c , agencyTeam.User__c);                
             }
        }
       
        AccountTeamMember[] accTeamMember = [select AccountId, UserId, TeamMemberRole from AccountTeamMember where AccountId in:  accountTeamSearch_Map.keyset() and UserId in:  accountTeamSearch_Map.values()];
             
        if(accTeamMember.size()>0){
            delete(accTeamMember);
        }
       
               
   }
}